﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Timers;


namespace WindowsWeb
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //线程1获取上网外IP，并发邮件
            Thread getComputerInfoThread = new Thread(new ThreadStart(GetIp));
            getComputerInfoThread.Start();
        }

        protected override void OnStop()
        {
        }
        private void GetIp()
        {
            System.Timers.Timer timerThread = new System.Timers.Timer();
            //设置timer可用
            timerThread.Enabled = true;

            //设置timer
            int computerInfoEnable =10;
            try
            {
                int.TryParse(System.Configuration.ConfigurationSettings.AppSettings["ComputerInfoTimes"], out computerInfoEnable);
            }
            catch (System.Exception ex)
            {	
            }
            timerThread.Interval = computerInfoEnable*60000;//10分钟一次

            //设置是否重复计时，如果该属性设为False,则只执行timer_Elapsed方法一次。
            timerThread.AutoReset = true;
            timerThread.Elapsed += new System.Timers.ElapsedEventHandler(getComputerInfo);
            timerThread.Start();
        }
        private void getComputerInfo(object sender, System.Timers.ElapsedEventArgs e)
        {
            string computerInfoEnable = System.Configuration.ConfigurationSettings.AppSettings["ComputerInfoEnable"];
            if (computerInfoEnable != "0")
            {
                try
                {
                    string a = ComputerInfo.Current.getComputerName();
                    string oldWebIP = LogHelper.Current.ReadIPString();
                    string newWebIP = ComputerInfo.Current.netIP();
                    if (newWebIP != "")
                    {
                        if (newWebIP != oldWebIP)
                        {
                            LogHelper.Current.WriteIPString(newWebIP);
                            SendMail.Current.sendMail("神级号起航", newWebIP, null);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }
    }
}
